package com.toolconclusion.time;

import java.time.*;
import java.time.temporal.TemporalAdjusters;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;

public class DateTimeUtils {

    public static String getGreetingTimeStr() {
        LocalTime now = LocalTime.now();
        if (now.isBefore(LocalTime.of(4, 0))) {
            return "晚上";
        } else if (now.isBefore(LocalTime.of(11, 0))) {
            return "早上";
        } else if (now.isBefore(LocalTime.of(14, 0))) {
            return "中午";
        } else if (now.isBefore(LocalTime.of(18, 0))) {
            return "下午";
        } else {
            return "晚上";
        }
    }

    public static LocalDate getFirstDayInMonth(LocalDate date) {
        return date.with(TemporalAdjusters.firstDayOfMonth());
    }

    public static LocalDate getLastDayInMonth(LocalDate date) {
        return date.with(TemporalAdjusters.lastDayOfMonth());
    }

    public static LocalDate getFirstDayInYear(LocalDate date) {
        return date.with(TemporalAdjusters.firstDayOfYear());
    }

    public static LocalDate getLastDayInYear(LocalDate date) {
        return date.with(TemporalAdjusters.lastDayOfYear());
    }

    public static LocalDateTime getStartTimeInMonth(LocalDate date) {
        LocalDate firstDay = getFirstDayInMonth(date);
        return LocalDateTime.of(firstDay, LocalTime.MIN);
    }

    public static LocalDateTime getEndTimeInMonth(LocalDate date) {
        LocalDate lastDay = getLastDayInMonth(date);
        return LocalDateTime.of(lastDay, LocalTime.MAX);
    }

    public static LocalDateTime getStartTimeInYear(LocalDate date) {
        LocalDate firstDay = getFirstDayInYear(date);
        return LocalDateTime.of(firstDay, LocalTime.MIN);
    }

    public static LocalDateTime getEndTimeInYear(LocalDate date) {
        LocalDate lastDay = getLastDayInYear(date);
        return LocalDateTime.of(lastDay, LocalTime.MAX);
    }

    public static Set<LocalDate> getMonthDateSet(LocalDate from, LocalDate to) {
        from = getFirstDayInMonth(from);
        to = getFirstDayInMonth(to);
        Set<LocalDate> monthDateSet = new HashSet<>();
        if (!from.isBefore(to)) {
            return monthDateSet;
        }
        while (!from.isAfter(to)) {
            monthDateSet.add(from);
            from = from.plusMonths(1);
        }
        return monthDateSet;
    }

    public static Set<LocalDate> getHistoryMonthDateSet(int monthCount) {
        LocalDate toDate = LocalDate.now();
        LocalDate fromDate = toDate.minusMonths(monthCount - 1);
        return getMonthDateSet(fromDate, toDate);
    }

    public static Date localDate2Date(LocalDate localDate) {
        if (null == localDate) {
            return null;
        }
        ZonedDateTime zonedDateTime = localDate.atStartOfDay(ZoneId.systemDefault());
        return Date.from(zonedDateTime.toInstant());

    }

    public static Set<LocalDate> getYearDateSet(LocalDate from, LocalDate to) {
        from = getFirstDayInYear(from);
        to = getFirstDayInYear(to);
        Set<LocalDate> yearDateSet = new HashSet<>();
        if (!from.isBefore(to)) {
            return yearDateSet;
        }
        while (!from.isAfter(to)) {
            yearDateSet.add(from);
            from = from.plusYears(1);
        }
        return yearDateSet;
    }

    public static Set<LocalDate> getHistoryYearDateSet(int yearCount) {
        LocalDate toDate = LocalDate.now();
        LocalDate fromDate = toDate.minusYears(yearCount - 1);
        return getYearDateSet(fromDate, toDate);
    }

    public static LocalDateTime getStartOfDay(LocalDate date) {
        return date.atStartOfDay();
    }

    public static LocalDateTime getEndOfDay(LocalDate date) {
        return date.atTime(LocalTime.MAX);
    }
}
